﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;
using System.Text;

namespace ZRegex
{

    public static class RegexUtility
    {
      
        private static StringBuilder s_CachedStringBuilder = null;


        /// <summary>
        /// 只做判断
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public static bool IsName( string input )
        {
            bool results = false;
            string pattern = "^[0-9a-zA-Z_-]{3,16}$";
            results = Regex.IsMatch(input, pattern);
            return results;
        }

        /// <summary>
        /// 可以限定长度
        /// </summary>
        /// <param name="input"></param>
        /// <param name="minLength">[闭区间</param>
        /// <param name="maxLength">]闭区间</param>
        /// <returns></returns>
        public static bool IsName( string input, int minLength, int maxLength)
        {
            bool results = false;

            CheckCachedStringBuilder();
            s_CachedStringBuilder.Length = 0;
            s_CachedStringBuilder.AppendFormat("^[0-9a-zA-Z_-]{{0},{1}}$", minLength, maxLength);
            string pattern = s_CachedStringBuilder.ToString();
            Regex regex = new Regex(pattern);
            results = regex.IsMatch( input );

            return results;
        }

        /// <summary>
        /// 返回信息码
        /// </summary>
        /// <param name="input"></param>
        /// <returns> 0 正常  1 长度越界 2 字符不合法</returns>
        public static int IsNameCode(string input)
        {
            int results = 0;

            if (IsName(input))
            {
                results = 0;
            }
            else
            {
                if (input.Length < 3 || input.Length > 16)
                {
                    results = 1;
                }
                else
                {
                    string pattern = "^[0-9a-zA-Z_-]*$";
                    Regex regex = new Regex(pattern);
                    if (!regex.IsMatch(input)) { results = 2; }
                    //results = 2;
                }
            }
            return results;
        }

        /// <summary>
        /// 返回信息码
        /// </summary>
        /// <param name="input"></param>
        /// <param name="minLength">[闭区间</param>
        /// <param name="maxLength">]闭区间</param>
        /// <returns></returns>
        public static int IsNameCode(string input, int minLength, int maxLength)
        {
            int results = 0;

            if (IsName(input, minLength, maxLength))
            {
                results = 0;
            }
            else
            {
                if (input.Length < minLength || input.Length > maxLength)
                {
                    results = 1;
                }
                else
                {
                    string pattern = "^[0-9a-zA-Z_-]*$";
                    Regex regex = new Regex(pattern);
                    if (!regex.IsMatch(input)) { results = 2; }
                    //results = 2;
                }
            }
            return results;
        }
     
        /// <summary>
        /// 电子邮件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsEmail(string input)
        {
            bool results = false;

            string pattern = "^[A-Za-z0-9\u4e00-\u9fa5]+@$[A-Za-z0-9-_]+(/.[A-Za-z0-9-_]+)+$";
            Regex regex = new Regex(pattern);
            results = regex.IsMatch(input);

            return results;
        }

        /// <summary>
        /// 电话号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsPhone(string input)
        {
            bool results = false;

            string pattern = "^/+?[0-9]{3,}$";
            Regex regex = new Regex(pattern);
            results = regex.IsMatch(input);

            return results;
        }

        /// <summary>
        /// 判断Url
        /// </summary>
        public static bool IsUrl(string input)
        {
            bool results = false;

            string pattern = "^((https?|ftp|file):////)?([/da-z/.-]+)/.([a-z/.]{2,6})([///w /.-]*)*//?$";
            Regex regex = new Regex(pattern);
            results = regex.IsMatch(input);

            return results;
        }

        /// <summary>
        /// 正整数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsPositiveInteger(string input)
        {
            bool results = false;

            string pattern = "^[0-9]+$";
            Regex regex = new Regex(pattern);
            results = regex.IsMatch(input);

            return results;
        }

        /// <summary>
        /// 负整数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNegativeInteger(string input)
        {
            bool results = false;

            string pattern = "^-[0-9]+$";
            Regex regex = new Regex(pattern);
            results = regex.IsMatch(input);

            return results;
        }

        /// <summary>
        /// 整数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsInteger(string input)
        {
            bool results = false;

            string pattern = "^-?[0-9]+$";
            Regex regex = new Regex(pattern);
            results = regex.IsMatch(input);

            return results;
        }

        /// <summary>
        /// 正数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsPositiveNumber(string input)
        {
            bool results = false;

            string pattern = "^[0-9]*/.?[0-9]+$";
            Regex regex = new Regex(pattern);
            results = regex.IsMatch(input);

            return results;
        }

        /// <summary>
        /// 负数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNegativeNumber(string input)
        {
            bool results = false;

            string pattern = "^-[0-9]*/.?[0-9]+$";
            Regex regex = new Regex(pattern);
            results = regex.IsMatch(input);

            return results;
        }

        /// <summary>
        /// 数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumber(string input)
        {
            bool results = false;

            string pattern = "^-?[0-9]*/.?[0-9]+$";
            results = Regex.IsMatch(input, pattern);

            return results;
        }


        /// <summary>
        /// 检查StringBuilder
        /// </summary>
        private static void CheckCachedStringBuilder()
        {
            if (s_CachedStringBuilder == null)
            {
                s_CachedStringBuilder = new StringBuilder(1024);
            }
        }
    }

}